from matplotlib import pyplot as plt
from wordcloud import WordCloud,ImageColorGenerator,STOPWORDS
from PIL import Image
import numpy as np

def cloud():
    # #    字体
    # font = r'C:\Windows\Fonts\HYZhongSongJ.ttf'

    text = (open(r'POM.txt','r',encoding='utf-8')).read()
    img = Image.open(r'cloud.jpg')  # 打开图片
    img_array = np.array(img)  # 将图片装换为数组

    # stopwords = set(STOPWORDS)
    # stopwords.add('xa0')  # 设置停止词，也就是你不想显示的词

    wc = WordCloud(
        background_color='white',
        mask=img_array,
        # font_path=font,
        # stopwords=stopwords,
        collocations=False
    )

    image_colors = ImageColorGenerator(img_array)
    wc.generate_from_text(text)  # 绘制图片
    plt.imshow(wc.recolor(color_func=image_colors), interpolation="bilinear")
    plt.axis('off')  # 横纵坐标是否显示在图上，一般要关闭
    # plt.figure()
    plt.show()  # 显示图片
    filename = r'wordCloud_POM.png'
    wc.to_file(filename)  # 保存图片

if __name__ == '__main__':
    cloud()